package com.kyzh.core.adapters

import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.beans.Game1
import com.kyzh.core.R
import com.kyzh.core.pager.gamedetail.GameDetailActivity1
import com.kyzh.core.databinding.HomeItemJingxuanBinding
import com.kyzh.core.utils.SampleCoverVideo
import com.kyzh.core.utils.setVisibility
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView

class HomeChosenAdapter(beans:MutableList<Game1>,var mPlayTag :String ):BaseQuickAdapter<Game1,BaseDataBindingHolder<HomeItemJingxuanBinding>>(R.layout.home_item_jingxuan,beans) {
    override fun convert(
        holder: BaseDataBindingHolder<HomeItemJingxuanBinding>,
        item: Game1
    ) {
        holder.dataBinding?.data = item

        holder?.apply {
            dataBinding?.root?.setOnClickListener {
                GameDetailActivity1.start(context,item.id)
            }
            setText(R.id.tv_tip,"  ${item.point}分   ${item.type}  ${item.size}")
            val bq = getView<RecyclerView>(R.id.bq)
            if (!item.biaoqian.isNullOrEmpty()) {
                bq.adapter = BiaoqianAdapter(item.biaoqian)
            } else {
                bq.setVisibility(false)
            }
            if(item.zhekou.isNullOrEmpty()){
                dataBinding?.tvZhekou?.setVisibility(false)
            }else{
                if(!item.zhekou.contains("折")){
                    dataBinding?.tvZhekou?.text = item.zhekou+"折"
                }else{
                    dataBinding?.tvZhekou?.text = item.zhekou
                }
                dataBinding?.tvZhekou?.setVisibility(true)
                if(item.zhekou.contains("10")){
                    dataBinding?.tvZhekou?.setVisibility(false)
                }
            }
            if(position >0){
                holder.dataBinding?.video?.setVisibility(false)
            }else{
                holder.dataBinding?.video?.setVisibility(true)
            }
            if(!item.video . isNullOrEmpty()){
                setVisible(R.id.bigImg,false)
                setVisible(R.id.videoPlayer,true)
                val videoPlayer = getView<SampleCoverVideo>(R.id.videoPlayer)
                videoPlayer.apply {
                    if(!item.image.isNullOrEmpty()) {
                        val imageView = ImageView(context)
                        Glide.with(context).load(item.image) .apply( RequestOptions()
                            .transforms( CenterCrop(),  RoundedCorners(15)
                            )).into(imageView)
                        thumbImageView = imageView

                    }
                    playPosition = holder.layoutPosition
                    playTag = mPlayTag
                    isNeedShowWifiTip = false
                    isLooping = true
                    dismissControlTime = 2000
                    setIsTouchWiget(true)
                    setThumbPlay(true)
                    backButton.visibility = View.GONE
                    backButton.setOnClickListener {
                        GSYVideoManager.backFromWindowFull(context)
                    }
                    fullscreenButton.setOnClickListener {
                        backButton.visibility = View.VISIBLE
                        startWindowFullscreen(context,false,false)
                    }
                    startButton.setOnClickListener {
                        when (currentState) {
                            GSYVideoView.CURRENT_STATE_PAUSE -> {
                                onVideoResume(false)
                            }
                            GSYVideoView.CURRENT_STATE_PLAYING -> {
                                onVideoPause()
                            }
                            else -> {
                                startPlayLogic()
                            }
                        }
                    }
                    isLockLand = true;
                    isAutoFullWithSize = false
                    isShowFullAnimation = true;
                    backButton.visibility = View.GONE;
                    fullscreenButton.setOnClickListener {
                        //第一个true是否需要隐藏actionbar，第二个true是否需要隐藏statusbar
                        videoPlayer.startWindowFullscreen(context, true, true);
                    }
                    setVideoAllCallBack(object : GSYSampleCallBack() {
                        override fun onPrepared(url: String?, vararg objects: Any?) {
                            super.onPrepared(url, *objects)
                            val full: Boolean =
                                videoPlayer.isIfCurrentIsFullscreen()
                            if (!videoPlayer
                                    .isIfCurrentIsFullscreen()
                            ) {
//                                GSYVideoManager.instance().isNeedMute = true
                            }
                            if (videoPlayer.isIfCurrentIsFullscreen()) {
                                GSYVideoManager.instance().setLastListener(videoPlayer)
                            }
                        }
                        override fun onEnterFullscreen(url: String?, vararg objects: Any?) {
                            super.onEnterFullscreen(url, *objects)
//                            GSYVideoManager.instance().isNeedMute = false
                        }
                        override fun onQuitFullscreen(url: String?, vararg objects: Any?) {
                            super.onQuitFullscreen(url, *objects)
//                            GSYVideoManager.instance().isNeedMute = true
                        }
                    })
                }
                videoPlayer.setUp(item.video, true,  "")
            }else if(!item.image . isNullOrEmpty()){
                setVisible(R.id.bigImg,true)
                setVisible(R.id.videoPlayer,false)
            }else{
                holder.dataBinding?.video?.setVisibility(false)
            }
        }
    }
}
